//
//  AddViewController.m
//  UI__相册三代版
//
//  Created by dllo on 16/4/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AddViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


//注意  :  获取系统相册一定要签订这两个协议
@interface AddViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation AddViewController

-(void)dealloc{
    [_name release];
    [_number release];
    [_imageButton release];
    [_myImage release];
    Block_release(_myBlock);
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //这是左按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    
    self.imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.imageButton.backgroundColor = [UIColor cyanColor];
    self.imageButton.frame = CGRectMake(WIDTH / 2 - 50, 100, 100, 100);
    [self.view addSubview:self.imageButton];
    [self.imageButton addTarget:self action:@selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //改成圆的
    self.imageButton.layer.masksToBounds = YES;
    self.imageButton.layer.cornerRadius = 50;
    
    self.name = [[UITextField alloc]initWithFrame:CGRectMake(WIDTH / 2 - 100, 250, 200, 50)];
    self.name.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.name];
    [_name release];
    
    self.number = [[UITextField alloc]initWithFrame:CGRectMake(WIDTH / 2 - 100, 350, 200, 50)];
    self.number.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.number];
    [_number release];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(rightBarButtonAction)];
    
    
    
    
    // Do any additional setup after loading the view.
}

//右按钮
-(void)rightBarButtonAction{
    ListModel *model = [[ListModel alloc] init];
    model.image = self.myImage;
    model.name = self.name.text;
    model.number = self.number.text;
    
#pragma mark  block第二步:  调用block
    self.myBlock(model);
    
    
}



-(void)imageButtonAction:(UIButton *)button{

  //获取系统相册
    UIImagePickerController *controll = [[UIImagePickerController alloc]init];
    controll.sourceType = UIImagePickerControllerSourceTypePhotoLibrary ;
    controll.allowsEditing = YES;
    controll.navigationBar.barStyle = UIBarStyleBlack;
    [controll.navigationBar setTintColor:[UIColor whiteColor]];
    
    controll.delegate = self;
   [self presentViewController:controll animated:YES completion:^{
       
       
   }];
    
    [controll release];
    
}







-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    //这是拿图片
    UIImage *image = [info valueForKey:@"UIImagePickerControllerEditedImage"];
    [self.imageButton setBackgroundImage:image forState:UIControlStateNormal];
    
    //接收选中的图片
    self.myImage = image;
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
        
    }];

}



-(void)leftAction:(UIBarButtonItem *)item{
//取消模态就是返回
[self dismissViewControllerAnimated:YES completion:^{
    
    
}];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
